package com.collectplus.express.view;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;

/**
 * 解决首页HomeFragment, 滑动事件以后还会触发onClickListener的bug
 * 
 * @author coffee<br>
 *         2016-11-28下午5:02:39
 */
public class OnTouchListenerExt implements OnTouchListener {

	private int mTouchSlop;

	public OnTouchListenerExt(Activity context) {
		mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
	}

	float x1, y1, x2, y2;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			x1 = event.getX();
			y1 = event.getY();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			x2 = event.getX();
			y2 = event.getY();
			if (Math.abs(x1 - x2) <= mTouchSlop && Math.abs(y1 - y2) <= mTouchSlop) {
				// 调用click
				v.performClick();
				return false;// 距离较小，当作click事件来处理
			} else { // 真正的onTouch事件
				return true;
			}
		}
		return true;// 返回true，不执行click事件
	}

}
